home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 24 / CU Amiga Magazine's Super CD-ROM 24 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-07].iso / CUCD / Programming / SWI / source / src / pl-stream.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-15  |  7.7 KB  |  247 lines

  1. /*  $Id: pl-stream.h,v 1.15 1998/04/15 15:17:10 jan Exp $
  2.  
  3.     Copyright (c) 1990 Jan Wielemaker. All rights reserved.
  4.     See ../LICENCE to find out about your rights.
  5.     jan@swi.psy.uva.nl
  6.  
  7.     Purpose:        SWI-Prolog IO streams
  8.     Last modified:    Wed Apr 03 18:07:18 1996
  9.             Better Win32 synchronisation, notably S__iob[]
  10. */
  11.  
  12. #ifndef _PL_STREAM_H
  13. #define _PL_STREAM_H
  14.  
  15. #include <stdarg.h>
  16.  
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20.  
  21. /* Get export declarations right.  Also in SWI-Prolog.h, hence the
  22.    check to avoid doing it twice.
  23. */
  24.  
  25. #ifndef _PL_EXPORT_DONE
  26. #define _PL_EXPORT_DONE
  27. #ifdef WIN32
  28. #ifndef __WIN32__
  29. #define __WIN32__
  30. #endif
  31. #endif
  32.  
  33. #if defined(__WIN32__) && !defined(__LCC__)
  34. #ifdef PL_KERNEL
  35. #define __pl_export     _declspec(dllexport)
  36. #define __pl_export_data _declspec(dllexport)
  37. #define install_t     void
  38. #else
  39. #define __pl_export     extern
  40. #define __pl_export_data _declspec(dllimport)
  41. #define install_t     _declspec(dllexport) void
  42. #endif
  43. #else /*__WIN32__*/
  44. #define __pl_export     extern
  45. #define __pl_export_data extern
  46. #define install_t     void
  47. #endif /*__WIN32__*/
  48. #endif /*_PL_EXPORT_DONE*/
  49.  
  50.  
  51. #ifndef EOF
  52. #define EOF (-1)
  53. #endif
  54.  
  55. #ifndef NULL
  56. #define NULL ((void *)0)
  57. #endif
  58.  
  59. #define SIO_BUFSIZE    (4096)        /* buffering buffer-size */
  60. #define SIO_LINESIZE    (1024)        /* Sgets() default buffer size */
  61. #define SIO_MAGIC    (7212676)    /* magic number */
  62.  
  63. typedef int   (*Sread_function)(void *handle, char *buf, int bufsize);
  64. typedef int   (*Swrite_function)(void *handle, char*buf, int bufsize);
  65. typedef long  (*Sseek_function)(void *handle, long pos, int whence);
  66. typedef int   (*Sclose_function)(void *handle);
  67.  
  68. typedef struct io_functions
  69. { Sread_function    read;        /* fill the buffer */
  70.   Swrite_function    write;        /* empty the buffer */
  71.   Sseek_function    seek;        /* seek to position */
  72.   Sclose_function    close;        /* close stream */
  73. } IOFUNCTIONS;
  74.  
  75. typedef struct io_position
  76. { long            charno;        /* character position in file */
  77.   int            lineno;        /* lineno in file */
  78.   int            linepos;    /* position in line */
  79. } IOPOS;
  80.  
  81. typedef struct io_stream
  82. { char               *bufp;        /* `here' */
  83.   char               *limitp;        /* read/write limit */
  84.   char               *buffer;        /* the buffer */
  85.   char               *unbuffer;    /* Sungetc buffer */
  86.   int            lastc;        /* last character written */
  87.   int            magic;        /* magic number SIO_MAGIC */
  88.   int              bufsize;    /* size of the buffer */
  89.   int            flags;        /* Status flags */
  90.   IOPOS            posbuf;        /* location in file */
  91.   IOPOS *        position;    /* pointer to above */
  92.   void               *handle;        /* function's handle */
  93.   IOFUNCTIONS           *functions;    /* open/close/read/write/seek */
  94.   int                locks;        /* lock/unlock count */
  95. } IOSTREAM;
  96.  
  97. #define SmakeFlag(n)    (1<<(n-1))
  98.  
  99. #define SIO_FBUF    SmakeFlag(1)    /* full buffering */
  100. #define SIO_LBUF    SmakeFlag(2)    /* line buffering */
  101. #define SIO_NBUF    SmakeFlag(3)    /* no buffering */
  102. #define SIO_FEOF    SmakeFlag(4)    /* end-of-file */
  103. #define SIO_FERR    SmakeFlag(5)    /* error ocurred */
  104. #define SIO_USERBUF    SmakeFlag(6)    /* buffer is from user */
  105. #define SIO_INPUT    SmakeFlag(7)    /* input stream */
  106. #define SIO_OUTPUT    SmakeFlag(8)    /* output stream */
  107. #define SIO_NOLINENO    SmakeFlag(9)    /* line no. info is void */
  108. #define SIO_NOLINEPOS    SmakeFlag(10)    /* line pos is void */
  109. #define SIO_STATIC    SmakeFlag(11)    /* Stream in static memory */
  110. #define SIO_RECORDPOS    SmakeFlag(12)    /* Maintain position */
  111. #define SIO_FILE    SmakeFlag(13)    /* Stream refers to an OS file */
  112. #define SIO_PIPE    SmakeFlag(14)    /* Stream refers to an OS pipe */
  113. #define SIO_NOFEOF    SmakeFlag(15)    /* don't set SIO_FEOF flag */
  114. #define SIO_TEXT    SmakeFlag(16)    /* text-mode operation */
  115. #define SIO_FEOF2    SmakeFlag(17)    /* attempt to read past eof */
  116. #define SIO_FEOF2ERR    SmakeFlag(18)    /* Sfpasteof() */
  117. #define SIO_NOCLOSE     SmakeFlag(19)    /* Do not close on abort */
  118.  
  119. #define    SIO_SEEK_SET    0    /* From beginning of file.  */
  120. #define    SIO_SEEK_CUR    1    /* From current position.  */
  121. #define    SIO_SEEK_END    2    /* From end of file.  */
  122.  
  123. __pl_export IOSTREAM *S__getiob(void);    /* get DLL's S__iob[] address */
  124.  
  125. __pl_export_data IOFUNCTIONS Sfilefunctions;    /* OS file functions */
  126. __pl_export_data int         Slinesize;        /* Sgets() linesize */
  127. __pl_export_data IOSTREAM    S__iob[];        /* Libs standard streams */
  128.  
  129. #define Sinput  (&S__iob[0])        /* Stream Sinput */
  130. #define Soutput (&S__iob[1])        /* Stream Soutput */
  131. #define Serror  (&S__iob[2])        /* Stream Serror */
  132.  
  133. #define Sgetchar()    Sgetc(Sinput)
  134. #define Sputchar(c)    Sputc((c), Soutput)
  135.  
  136. #define S__updatefilepos(s, c) \
  137.     ((s)->position ? S__fupdatefilepos((s)->position, (c)) \
  138.                : (c))
  139.  
  140. #define Snpgetc(s) ((s)->bufp < (s)->limitp ? (int)(*(s)->bufp++)&0xff \
  141.                         : S__fillbuf(s))
  142. #define Sgetc(s) S__updatefilepos((s), Snpgetc(s))
  143.  
  144. #if IOSTREAM_REPLACES_STDIO
  145.  
  146. #undef FILE
  147. #undef stdin
  148. #undef stdout
  149. #undef stderr
  150. #undef putc
  151. #undef getc
  152. #undef putchar
  153. #undef getchar
  154. #undef feof
  155. #undef ferror
  156. #undef fileno
  157. #undef clearerr
  158.  
  159. #define FILE        IOSTREAM
  160. #define stdin        Sinput
  161. #define stdout        Soutput
  162. #define stderr        Serror
  163.  
  164. #define    putc        Sputc
  165. #define    getc        Sgetc
  166. #define    fputc        Sputc
  167. #define    fgetc        Sgetc
  168. #define getw        Sgetw
  169. #define putw        Sputw
  170. #define fread        Sfread
  171. #define fwrite        Sfwrite
  172. #define    ungetc        Sungetc
  173. #define putchar        Sputchar
  174. #define getchar        Sgetchar
  175. #define feof        Sfeof
  176. #define ferror        Sferror
  177. #define clearerr    Sclearerr
  178. #define    fflush        Sflush
  179. #define    fseek        Sseek
  180. #define    ftell        Stell
  181. #define    fclose        Sclose
  182. #define fgets        Sfgets
  183. #define gets        Sgets
  184. #define    fputs        Sfputs
  185. #define    puts        Sputs
  186. #define    fprintf        Sfprintf
  187. #define    printf        Sprintf
  188. #define    vprintf        Svprintf
  189. #define    vfprintf    Svfprintf
  190. #define    sprintf        Ssprintf
  191. #define    vsprintf    Svsprintf
  192. #define fopen        Sopen_file
  193. #define fdopen        Sfdopen
  194. #define    fileno        Sfileno
  195. #define popen        Sopen_pipe
  196.  
  197. #endif /*IOSTREAM_REPLACES_STDIO*/
  198.  
  199.          /*******************************
  200.          *        PROTOTYPES        *
  201.          *******************************/
  202.  
  203. __pl_export int        S__fupdatefilepos(IOPOS *p, int c);
  204. __pl_export int        S__fillbuf(IOSTREAM *s);
  205. __pl_export int        Sputc(int c, IOSTREAM *s);
  206. __pl_export int        Sfgetc(IOSTREAM *s);
  207. __pl_export int        Sungetc(int c, IOSTREAM *s);
  208. __pl_export int        Sputw(int w, IOSTREAM *s);
  209. __pl_export int        Sgetw(IOSTREAM *s);
  210. __pl_export int        Sfread(void *data, int size, int elems, IOSTREAM *s);
  211. __pl_export int        Sfwrite(void *data, int size, int elems, IOSTREAM *s);
  212. __pl_export int        Sfeof(IOSTREAM *s);
  213. __pl_export int        Sfpasteof(IOSTREAM *s);
  214. __pl_export int        Sferror(IOSTREAM *s);
  215. __pl_export void    Sclearerr(IOSTREAM *s);
  216. __pl_export int        Sflush(IOSTREAM *s);
  217. __pl_export long    Sseek(IOSTREAM *s, long pos, int whence);
  218. __pl_export long    Stell(IOSTREAM *s);
  219. __pl_export int        Sclose(IOSTREAM *s);
  220. __pl_export char *    Sfgets(char *buf, int n, IOSTREAM *s);
  221. __pl_export char *    Sgets(char *buf);
  222. __pl_export int        Sfputs(const char *q, IOSTREAM *s);
  223. __pl_export int        Sputs(const char *q);
  224. __pl_export int        Sfprintf(IOSTREAM *s, const char *fm, ...);
  225. __pl_export int        Sprintf(const char *fm, ...);
  226. __pl_export int        Svprintf(const char *fm, va_list args);
  227. __pl_export int        Svfprintf(IOSTREAM *s, const char *fm, va_list args);
  228. __pl_export int        Ssprintf(char *buf, const char *fm, ...);
  229. __pl_export int        Svsprintf(char *buf, const char *fm, va_list args);
  230. __pl_export int        Svdprintf(const char *fm, va_list args);
  231. __pl_export int        Sdprintf(const char *fm, ...);
  232. __pl_export int        Slock(IOSTREAM *s);
  233. __pl_export int        Sunlock(IOSTREAM *s);
  234. __pl_export IOSTREAM *    Snew(void *handle, int flags, IOFUNCTIONS *functions);
  235. __pl_export IOSTREAM *    Sopen_file(const char *path, char *how);
  236. __pl_export IOSTREAM *    Sfdopen(int fd, char *type);
  237. __pl_export int           Sfileno(IOSTREAM *s);
  238. __pl_export IOSTREAM *    Sopen_pipe(const char *command, const char *type);
  239. __pl_export IOSTREAM *    Sopenmem(char **buffer, int *sizep, const char *mode);
  240. __pl_export IOSTREAM *    Sopen_string(IOSTREAM *s, char *buf, int sz, char *m);
  241.  
  242. #ifdef __cplusplus
  243. }
  244. #endif
  245.  
  246. #endif /*_PL_STREAM_H*/
  247.